package org.jeecg.modules.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.ChineseCharacterUtil;
import org.jeecg.modules.flower.entity.FlowerStorehouse;
import org.jeecg.modules.flower.entity.FlowerStorehouseDetail;
import org.jeecg.modules.flower.entity.FlowerVariety;
import org.jeecg.modules.flower.mapper.FlowerStorehouseDetailMapper;
import org.jeecg.modules.flower.mapper.FlowerStorehouseMapper;
import org.jeecg.modules.flower.service.IFlowerStorehouseService;
import org.jeecg.modules.flower.util.CommonUtil;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 仓库主表
 * @Author: jeecg-boot
 * @Date:   2022-07-15
 * @Version: V1.0
 */
@Service
public class FlowerStorehouseServiceImpl extends ServiceImpl<FlowerStorehouseMapper, FlowerStorehouse> implements IFlowerStorehouseService {

	@Autowired
	private FlowerStorehouseMapper flowerStorehouseMapper;
	@Autowired
	private FlowerStorehouseDetailMapper flowerStorehouseDetailMapper;

	@Autowired
	private ISysUserDepartService sysUserDepartService;
	@Override


	@Transactional
	public void saveMain(FlowerStorehouse flowerStorehouse, List<FlowerStorehouseDetail> flowerStorehouseDetailList) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
		//自动生成编码
		flowerStorehouse.setCode(createCode(flowerStorehouse.getName()));
//		所属单位
		flowerStorehouse.setDeptCode( sysUser.getOrgCode());
		List<DepartIdModel> departIdModels = sysUserDepartService.queryDepartIdsOfUser(sysUser.getId());
		flowerStorehouse.setDeptName( departIdModels.get(0).getTitle());
		flowerStorehouseMapper.insert(flowerStorehouse);
		if(flowerStorehouseDetailList!=null && flowerStorehouseDetailList.size()>0) {
			for(FlowerStorehouseDetail entity:flowerStorehouseDetailList) {
				//外键设置
				entity.setMainid(flowerStorehouse.getId());
				flowerStorehouseDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FlowerStorehouse flowerStorehouse,List<FlowerStorehouseDetail> flowerStorehouseDetailList) {
		flowerStorehouseMapper.updateById(flowerStorehouse);
		
		//1.先删除子表数据
		flowerStorehouseDetailMapper.deleteByMainId(flowerStorehouse.getId());
		
		//2.子表数据重新插入
		if(flowerStorehouseDetailList!=null && flowerStorehouseDetailList.size()>0) {
			for(FlowerStorehouseDetail entity:flowerStorehouseDetailList) {
				//外键设置
				entity.setMainid(flowerStorehouse.getId());
				flowerStorehouseDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		flowerStorehouseDetailMapper.deleteByMainId(id);
		flowerStorehouseMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			flowerStorehouseDetailMapper.deleteByMainId(id.toString());
			flowerStorehouseMapper.deleteById(id);
		}
	}


	/**
	 *
	 *
	 * 根据人员id查询仓库
	 * @param personid
	 * @return
	 */
	@Override
    public List<FlowerStorehouse> queryListByPsn(String personid ,String baseid) {
		return baseMapper.queryListByPsn(personid,  baseid);
	}


	private String createCode (String name) {
		//自动生成仓库编号
		Integer leng = 2;
		String refex = ChineseCharacterUtil.getUpperCase(name, false);
		LambdaQueryWrapper<FlowerStorehouse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.likeRight(FlowerStorehouse::getCode, refex);
		lambdaQueryWrapper.orderByDesc(FlowerStorehouse::getCode);
		List<FlowerStorehouse> list = this.list(lambdaQueryWrapper);
		String oldNo = "";
		if (list.size() > 0) {
			oldNo = list.get(0).getCode();
		}
		Integer n = 1;
		if (StringUtils.isNotBlank(oldNo) && oldNo.length() > leng) {
			String oldNoNum = oldNo.substring(oldNo.length() - leng, oldNo.length());
			n = Integer.parseInt(oldNoNum) + 1;
		}
		String newFlowNum = CommonUtil.subLength(n, leng);
		return refex + newFlowNum;
	}
}
